<template>
  <div class="star-container">
    <!-- 标题和搜索框区域 -->
    <div class="header">
      <h2 class="star-title">我的收藏</h2>
      <div class="search-box">
        <input 
          type="text" 
          v-model="searchKeyword" 
          placeholder="搜索收藏" 
          class="search-input"
          @input="handleSearch"
        >
        <text class="search-icon">🔍</text>
      </div>
    </div>
    
    <!-- 空状态提示 -->
    <div v-if="filteredStars.length === 0" class="empty-tip">
      暂无收藏记录
    </div>
    
    <!-- 收藏列表 -->
    <ul v-else class="star-list">
      <li 
        v-for="item in filteredStars" 
        :key="item.id" 
        class="star-item"
        @click="viewStarDetail(item)"
      >
        <div class="star-content-container">
          <div class="star-content">{{ item.content }}</div>
          <button 
            class="unstar-btn" 
            @click.stop="removeStar(item.id)"
          >
            <text class="icon">★</text>
            <text class="text">取消收藏</text>
          </button>
        </div>
        <div class="star-time">{{ item.time }}</div>
      </li>
    </ul>
    
    <!-- 收藏详情弹窗 -->
    <div class="detail-mask" v-if="showDetail" @click="closeDetail"></div>
    <div class="detail-container" v-if="showDetail">
      <div class="detail-header">
        <h3 class="detail-title">对话详情</h3>
        <button class="close-btn" @click="closeDetail">×</button>
      </div>
      <div class="detail-content">
        <div class="detail-item user-item">
          <div class="role-label">问题：</div>
          <div class="message-content">{{ currentStar.question }}</div>
        </div>
        <div class="detail-item ai-item">
          <div class="role-label">回答：</div>
          <div class="message-content">{{ currentStar.answer }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      starList: [],
      filteredStars: [],
      searchKeyword: '',
      showDetail: false,
      currentStar: null
    }
  },
  onShow() {
    this.loadStars();
  },
  methods: {
    // 加载收藏记录
    loadStars() {
      const list = uni.getStorageSync('starList') || [];
      // 按时间戳倒序排列，最新的在前面
      this.starList = Array.isArray(list) ? list.sort((a, b) => b.id - a.id) : [];
      this.filteredStars = [...this.starList];
    },
    
    // 处理搜索逻辑（模糊查询，不区分大小写）
    handleSearch() {
      const keyword = this.searchKeyword.trim().toLowerCase();
      
      if (!keyword) {
        this.filteredStars = [...this.starList];
        return;
      }
      
      this.filteredStars = this.starList.filter(item => {
        const content = (item.content || '').toLowerCase();
        const question = (item.question || '').toLowerCase();
        const answer = (item.answer || '').toLowerCase();
        
        return content.includes(keyword) || 
               question.includes(keyword) || 
               answer.includes(keyword);
      });
    },
    
    // 移除收藏
    removeStar(id) {
      uni.showModal({
        title: '确认取消',
        content: '确定要取消收藏这条记录吗？',
        confirmText: '确认',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            this.starList = this.starList.filter(item => item.id !== id);
            this.filteredStars = this.filteredStars.filter(item => item.id !== id);
            uni.setStorageSync('starList', this.starList);
            
            uni.showToast({
              title: '已取消收藏',
              icon: 'success'
            });
          }
        }
      });
    },
    
    // 查看收藏详情
    viewStarDetail(item) {
      this.currentStar = item;
      this.showDetail = true;
    },
    
    // 关闭详情弹窗
    closeDetail() {
      this.showDetail = false;
      this.currentStar = null;
    }
  }
}
</script>

<style scoped>
.star-container {
  padding: 24rpx 20rpx;
  background: linear-gradient(135deg, #eaf4fe 0%, #b3d9ff 100%);
  min-height: 100vh;
}

/* 头部区域 */
.header {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  margin-bottom: 24rpx;
}

.star-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin: 0;
}

/* 搜索框样式 */
.search-box {
  position: relative;
  width: 100%;
}


.search-input {
  width: 100%;
  padding: 14px 14px; 
  font-size: 15px; 
  border: 1px solid #ddd; 
  border-radius: 12px; 
  background-color: #fff;
  box-sizing: border-box;
  outline: none;
  min-height: 50px; 
  padding-left: 70rpx; 
}

.search-input::placeholder {
  color: #ccc;
  font-size: 26rpx;
  letter-spacing: 0.5rpx;
}

.search-icon {
  position: absolute;
  left: 18rpx;
  top: 50%;
  transform: translateY(-50%);
  font-size: 26rpx;
  color: #999;
}

/* 空状态提示 */
.empty-tip {
  color: #aaa;
  font-size: 28rpx;
  text-align: center;
  margin-top: 80rpx;
}

/* 收藏列表 */
.star-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.star-item {
  background: #f0f7ff;;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(177, 223, 223, 0.942);
  margin-bottom: 18rpx;
  padding: 22rpx 18rpx;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.star-content-container {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.star-content {
  font-size: 30rpx;
  color: #222;
  margin-bottom: 8rpx;
  word-break: break-all;
  flex: 1;
  padding-right: 10rpx;
}

/* 取消收藏按钮 */
.unstar-btn {
  display: flex;
  align-items: center;
  background: none;
  border: 1px solid #ff4d4f;
  color: #ff4d4f;
  border-radius: 6rpx;
  padding: 5rpx 12rpx;
  font-size: 24rpx;
  cursor: pointer;
  white-space: nowrap;
}

.unstar-btn .icon {
  margin-right: 5rpx;
  color: #ff4d4f;
}

.star-time {
  font-size: 24rpx;
  color: #999;
  text-align: right;
}

/* 详情弹窗样式 */
.detail-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.detail-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90%;
  max-width: 600rpx;
  background-color: #fff;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.15);
  z-index: 1000;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.detail-header {
  padding: 24rpx;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 36rpx;
  color: #999;
  cursor: pointer;
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.detail-content {
  padding: 24rpx;
  overflow-y: auto;
  flex: 1;
}

.detail-item {
  margin-bottom: 30rpx;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.role-label {
  font-size: 28rpx;
  font-weight: 600;
  margin-bottom: 12rpx;
  color: #666;
}

.user-item .role-label {
  color: #1890ff;
}

.ai-item .role-label {
  color: #722ed1;
}

.message-content {
  font-size: 28rpx;
  line-height: 1.6;
  color: #333;
  padding: 20rpx;
  border-radius: 8rpx;
}

.user-item .message-content {
  background-color: #e6f7ff;
}

.ai-item .message-content {
  background-color: #f9f0ff;
}

/* 适配不同屏幕尺寸 */
@media (min-width: 768px) {
  .search-input {
    font-size: 30rpx;
    padding: 28rpx 24rpx 28rpx 60rpx;
  }
  
  .search-input::placeholder {
    font-size: 28rpx;
  }
  
  .search-icon {
    font-size: 28rpx;
    left: 20rpx;
  }
}
</style>